package lg.minecraft.plugin.pokemonmenu.gui;

import com.cobblemon.mod.common.api.pokemon.PokemonProperties;
import com.cobblemon.mod.common.api.storage.pc.PCBox;
import com.cobblemon.mod.common.api.storage.pc.PCStore;
import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.PokemonPlayer;
import lg.minecraft.plugin.pokemonmenu.manager.PokemonPlayerManger;
import lg.minecraft.plugin.pokemonmenu.util.Position;
import org.bukkit.Sound;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author langle__
 * @version 1.0
 */
public class SearchInventor extends LGInventory {

    private static final int[] GLASS_ARRAY = {45, 46, 47, 51, 52, 53};
    private static final int PAGE_SIZE = 45; // 每页显示的数量

    private final List<Position> pokemonList = new ArrayList<>();
    private final PokemonProperties properties;
    private final UUID targetUUID;
    private final int totalPages;

    private int page = 0;

    public SearchInventor(UUID targetUUID, PokemonProperties properties) {
        super(6, LGPokemonMenu.getMessage().getGuiTitleSearch());
        this.targetUUID = targetUUID;
        this.properties = properties;
        this.searchPokemon();
        this.totalPages = (pokemonList.size() + PAGE_SIZE - 1) / PAGE_SIZE;
    }

    public void searchPokemon() {
        PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(targetUUID);
        PCStore pcStore = pokemonPlayer.getPCStore();
        List<PCBox> boxes = pcStore.getBoxes();
        for (int page = 0, boxesSize = boxes.size(); page < boxesSize; page++) {
            PCBox pcBox = boxes.get(page);
            for (int slot = 0; slot < 30; slot++) {
                Pokemon pokemon = pcBox.get(slot);
                if (pokemon == null) continue;
                if (properties.matches(pokemon)) {
                    pokemonList.add(new Position(page, slot));
                }
            }
        }
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(targetUUID);
        inventory.clear();

        int startIndex = page * PAGE_SIZE;
        int endIndex = Math.min(startIndex + PAGE_SIZE, pokemonList.size());

        for (int i = 0; i < endIndex - startIndex; i++) {
            Position position = pokemonList.get(startIndex + i);
            Pokemon pokemon = pokemonPlayer.getPokemon(position);
            inventory.setItem(i, LGPokemonMenu.getGuiItem().getSearchPokemon(pokemon, position.getBox() + 1, position.getSlot() + 1));
        }

        for (int slot : GLASS_ARRAY) inventory.setItem(slot, LGPokemonMenu.getGuiItem().getBorder());
        inventory.setItem(49, LGPokemonMenu.getGuiItem().getBack());
        inventory.setItem(48, LGPokemonMenu.getGuiItem().getPreviousPage(page + 1, totalPages));
        inventory.setItem(50, LGPokemonMenu.getGuiItem().getNextPage(page + 1, totalPages));
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();
        PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(targetUUID);

        if (slot == 49) {
            pokemonPlayer.getPreviousInventory().openInventory(player);
            pokemonPlayer.setPreviousInventory(null);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot == 48 && page > 0) {
            page--;
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot == 50 && page < totalPages - 1) {
            page++;
            onInventoryOpen(context);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        } else if (slot >= 0 && slot < PAGE_SIZE) {
            int index = page * PAGE_SIZE + slot;
            if (index < pokemonList.size()) {
                Position position = pokemonList.get(index);
                new PCInventor(targetUUID, position.getBox()).openInventory(player);
                pokemonPlayer.setPreviousInventory(null);
                player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
            }
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
    }

    public void openInventory(Player player, LGInventory inventory) {
        PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(player);
        pokemonPlayer.setPreviousInventory(inventory);
        this.openInventory(player);
    }

}
